import { defineStore } from 'pinia'
import { getConfig } from '@/api'
import type { ConfigUserStatusResponseData, ConfigUserStatusValueType } from '@/types/api'

const id = 'config'

const store = defineStore(id, {
  state: () => ({
    userStatus: [] as ConfigUserStatusResponseData[]
  }),
  getters: {
    // 提供给单选框使用的数据，排除 All
    selectUserStatus(state) {
      return state.userStatus.filter((us) => us.value !== 'All')
    },
    userStatusLabelMap(state) {
      return state.userStatus.reduce(
        (acc, us) => ((acc[us.value] = us.label), acc),
        {} as Record<ConfigUserStatusValueType, string>
      )
    }
  },
  actions: {
    async refresh() {
      const { user_status } = await getConfig()
      this.userStatus = user_status
    },
    getUserStatus(value: ConfigUserStatusValueType) {
      return this.userStatus.find((sta) => sta.value === value)!
    }
  },
  persist: {
    key: id
  }
})

export default store
